package org.lzuploader.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.lzuploader.gui.listeners.CancelSelectionListener;
import org.lzuploader.gui.listeners.login.EnterKeyListener;
import org.lzuploader.gui.listeners.login.OkSelectionListener;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;
import org.lzuploader.model.account.Account;
import org.lzuploader.model.account.AccountServerConfig;

/** This class displays a window where the user can type in a username and a login.
 * 
 * @author Emmanuel Cron
 *
 */
public class WLogin
{
    /** Width of the window. */
    private static final int WINDOW_WIDTH = 235;
    /** Height of the window. */
    private static final int WINDOW_HEIGHT = 115;
    
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(WLogin.class);
    /** Localization of this class. */
    private static Localization localization = Localization.getInstance();
    
    /** Shell used to display this window. */
    private Shell shell;
    
    /* COMPONENTS */
    
    /** Text containing the login to use when connecting on the server. */ 
    private Text loginText;
    
    /** Text containing the password to used when connecting on the server. */
    private Text pwdText;
    
    /* END OF COMPONENTS */
    
    /** Creates the login window.
     * Creates the login window, initializing its controls and various settings
     * @param parent    Parent that called the window.
     */
    public WLogin(final Shell parent)
    {
        log.debug("Creating login window.");
        
        shell = new Shell(parent, SWT.APPLICATION_MODAL + SWT.DIALOG_TRIM);
        shell.setData(this);
        
        Position.centerElement(parent, shell, WLogin.WINDOW_WIDTH, WLogin.WINDOW_HEIGHT);
        shell.setText(localization.getString("DIALOG_TITLE_LOGIN"));
        
        // Populating window
        populateWindow();
        
        log.trace("Checking if user wishes to save his authentification.");
        final Config config = Config.getInstance();
        final Account account = config.getAccounts().get(config.getSelectedAccount());
        final AccountServerConfig accountServerConfig = account.getServerConfigs().get(account.getServer());
        
        log.trace("Opening window.");
        shell.open();
        
        if (accountServerConfig.isAuthentificationSaveable())
        {
            log.trace("User wishes to save it. Checking if login/pwd are already known.");
            
            if (accountServerConfig.getUsername() != null && accountServerConfig.getPassword() != null)
            {
                log.trace("Username/pwd are already know, filling fields and launching process.");
                
                loginText.setText(accountServerConfig.getUsername());
                pwdText.setText(accountServerConfig.getPassword());
                
                new OkSelectionListener().processAuthentificationAndUpload(shell);
            }
        }
    }
    
    /** Populates the main window with various controls.
     * 
     */
    private void populateWindow()
    {
        log.debug("Populating window.");
        
        final Label login = new Label(shell, SWT.RIGHT);
        login.setLocation(10, 10);
        login.setSize(85, 20);
        login.setText(localization.getString("LABEL_LOGIN") + ":");
        
        final Label pwd = new Label(shell, SWT.RIGHT);
        pwd.setLocation(10, 35);
        pwd.setSize(85, 20);
        pwd.setText(localization.getString("LABEL_PASSWORD") + ":");
        
        loginText = new Text(shell, SWT.BORDER);
        loginText.setLocation(105, 9);
        loginText.setSize(115, 20);
        loginText.addKeyListener(new EnterKeyListener());
        
        pwdText = new Text(shell, SWT.BORDER + SWT.PASSWORD);
        pwdText.setLocation(105, 34);
        pwdText.setSize(115, 20);
        pwdText.addKeyListener(new EnterKeyListener());
        
        final Button ok = new Button(shell, SWT.PUSH);
        ok.setText(localization.getString("BUTTON_OK"));
        ok.setSize(70, 25);
        ok.setLocation(75, 57);
        ok.addSelectionListener(new OkSelectionListener());
        
        final Button cancel = new Button(shell, SWT.PUSH);
        cancel.setText(localization.getString("BUTTON_CANCEL"));
        cancel.setSize(70, 25);
        cancel.setLocation(150, 57);
        cancel.addSelectionListener(new CancelSelectionListener());
    }
    
    /** Returns the login {@link Text} widget.
     * 
     * @return The login widget
     */
    public Text getLoginText()
    {
        return loginText;
    }
    
    /** Returns the password {@link Text} widget.
     * 
     * @return The password widget
     */
    public Text getPasswordText()
    {
        return pwdText;
    }
}
